package com.sign2024.sign2024project.projectservice.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.projectservice.HistoricalRelational;
import com.sign2024.sign2024core.entry.projectservice.SpecialtySignRelation;
import com.sign2024.sign2024core.entry.projecttask.RollbackRecord;
import com.sign2024.sign2024project.projectservice.mapper.SpecialtySignRelationMapper;
import com.sign2024.sign2024project.projectservice.param.RelationParam;
import com.sign2024.sign2024project.projectservice.service.SpecialtySignRelationService;
import com.sign2024.sign2024project.projecttask.mapper.RollbackRecordMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 专家专业与标段对应关系 业务层
 *
 * @author yujie
 * @data 2024/8/1 9:48
 */
@Service
public class SpecialtySignRelationServiceImpl implements SpecialtySignRelationService {

    @Resource
    private SpecialtySignRelationMapper specialtySignRelationMapper;

    @Resource
    private RollbackRecordMapper rollbackRecordMapper;


    @Override
    public List<SpecialtySignRelation> exportInfoList(RelationParam param) {
        return specialtySignRelationMapper.exportInfoList(param);
    }

    @Override
    public int updateSpecialtySignRelation(SpecialtySignRelation relation) {
        return specialtySignRelationMapper.updateSpecialtySignRelation(relation);
    }

    @Override
    public Page<SpecialtySignRelation> pageSpecialtySignRelation(RelationParam param) {
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<SpecialtySignRelation> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<SpecialtySignRelation> list = specialtySignRelationMapper.selectRelationByPage(param);

        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = specialtySignRelationMapper.pageRelationCount(param);
        page.setTotal(aLong);
        page.setRecords(list);

        return page;
    }

    @Override
    public List<SpecialtySignRelation> selectAllList() {
        return specialtySignRelationMapper.selectAllList();
    }

    @Override
    public void insertBatchSpecialtySignRelation(List<SpecialtySignRelation> entities) {
        // 数据为空 返回异常
        if (StringUtils.isEmpty(entities)) {
            throw new CommonException(CommonConstants.ERROR, "数据为空");
        }
        specialtySignRelationMapper.insertBatchSpecialtySignRelation(entities);
    }

    @Override
    public void rollbackHistorical() {
        // 目前历史回退需要做数据判断 这是的历史回退是 工程服务 直接 数据写死 为 0
//        selectRollbackOne
        RollbackRecord vo = rollbackRecordMapper.selectRollbackOne(0);
        // 判断 是否 已回退
        if (vo.getRollbackFlag() == 1) {
            throw new CommonException(CommonConstants.ERROR, "当前数据已回退上一版本，无法回退");
        }

        // 直接获取历史版本中 版本号最大的数据列
        List<HistoricalRelational> historical = specialtySignRelationMapper.selectHistoricalByMaxCode();
        if (!historical.isEmpty()) {
            // 数据 不为空
            // 将 这一版半历史数据 赋值到 关系表中
            // 清楚掉当前关系表中的数据
            specialtySignRelationMapper.cleanSpecialtySignRelation();
            for (HistoricalRelational ss : historical) {
                SpecialtySignRelation h = new SpecialtySignRelation();
                h.setMaterialCode(ss.getMaterialCode());
                h.setMaterialDescribe(ss.getMaterialDescribe());
                h.setExtendDescribe(ss.getExtendDescribe());
                h.setSignMajorOne(ss.getSignMajorOne());
                h.setSignMajorTwo(ss.getSignMajorTwo());
                h.setSignMajorThree(ss.getSignMajorThree());
                h.setSignMajorFour(ss.getSignMajorFour());
                specialtySignRelationMapper.insertSpecialtySignRelation(h);
            }
        }

        // 回退成功 将其 修改为已回退
        rollbackRecordMapper.updateRollbackFlag(vo.getId());

//        // 获取 历史版本中的 最新版本号
//        int code = 0;
//        Integer versionCode = specialtySignRelationMapper.countMaxVersionCode();
//        if (versionCode != null) {
//            code = versionCode + 1;
//        } else {
//            code = code + 1;
//        }
//
//        // 根据版本号批量添加专家关系 历史版本数据
//        // 先将历史版本最新的数据提取出来
//        // 删除现有版本的数据， 获取历史版本的数据
//
//        // 提取历史版本最新数据(数据添加考虑是否使用vo, 两个不同数据表，字段内容是一样的)
//        List<HistoricalRelational> historicalList = specialtySignRelationMapper.selectHistoricalByMaxCode();
//
//        // 往 专家对应关系同 添加数据
//        // 通过 vo 类 做数据处理
//
//        // 是否直接删除 专家表中 当前内容数据
//        specialtySignRelationMapper.deleteSpecialty();
//
//        // 将查询到的历史表数据 添加到对应表中
//        // 如果分别赋值 太 low 考虑其他方法
//        if (StringUtils.isNotEmpty(historicalList)) {
//            for (HistoricalRelational h : historicalList) {
//                SpecialtySignRelation signRelation = new SpecialtySignRelation();
//                BeanUtils.copyProperties(h, signRelation);
//
//                // 添加数据
//                specialtySignRelationMapper.insertSpecialtySignRelation(signRelation);
//            }
//        }
    }

    @Override
    public void addHistorical(List<SpecialtySignRelation> entities, String fileName) {
        if (entities.isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "excel导入失败");
        }


        // 清空表中数据
//        specialtySignRelationMapper.cleanHistorical();
        // 将 当前数据 放入到 历史版本中

        // 获取历史版本号
        int code = 0;
        Integer versionCode = specialtySignRelationMapper.countMaxVersionCode();
        if (versionCode != null) {
            code = versionCode + 1;
        } else {
            code = code + 1;
        }


        // 获取 当前 关系表中的数据
        List<SpecialtySignRelation> list = specialtySignRelationMapper.selectAllList();
        if (!list.isEmpty()) {
            for (SpecialtySignRelation ss :
                    list) {
                HistoricalRelational h = new HistoricalRelational();
                // 版本号是1
                h.setVersionCode(code);
                h.setMaterialCode(ss.getMaterialCode());
                h.setMaterialDescribe(ss.getMaterialDescribe());
                h.setExtendDescribe(ss.getExtendDescribe());
                h.setSignMajorOne(ss.getSignMajorOne());
                h.setSignMajorTwo(ss.getSignMajorTwo());
                h.setSignMajorThree(ss.getSignMajorThree());
                h.setSignMajorFour(ss.getSignMajorFour());
                specialtySignRelationMapper.insertHistoricalRelational(h);
            }
        } else {
            for (SpecialtySignRelation ss : entities) {
                HistoricalRelational h = new HistoricalRelational();
                // 版本号是1
                h.setVersionCode(code);
                h.setMaterialCode(ss.getMaterialCode());
                h.setMaterialDescribe(ss.getMaterialDescribe());
                h.setExtendDescribe(ss.getExtendDescribe());
                h.setSignMajorOne(ss.getSignMajorOne());
                h.setSignMajorTwo(ss.getSignMajorTwo());
                h.setSignMajorThree(ss.getSignMajorThree());
                h.setSignMajorFour(ss.getSignMajorFour());
                specialtySignRelationMapper.insertHistoricalRelational(h);
            }
        }


        // 做数据的清空
        specialtySignRelationMapper.cleanSpecialtySignRelation();
        // 添加数据
        for (SpecialtySignRelation ss : entities) {
            specialtySignRelationMapper.insertSpecialtySignRelation(ss);
        }

        // 添加 回退记录  taskState 任务标识
        RollbackRecord rollback = new RollbackRecord();

        rollback.setFileName(fileName);
        // 任务标识
        rollback.setTaskState(0);
        // 数据 版本号
        rollback.setVersionCode(code);
        rollbackRecordMapper.insert(rollback);
    }

    @Override
    public void cleanSpecialtySignRelation() {
        specialtySignRelationMapper.cleanSpecialtySignRelation();
    }
}
